//
//  FetusViewController.m
//  SHHCPhone
//
//  Created by Sheng long on 14-7-14.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "FetusViewController.h"
#import "TextFieldView.h"
#import "SelectPickerView.h"
#import "NSString+DifferentColorString.h"

@interface FetusViewController ()<UITextFieldDelegate,SelectPickerViewDelegate>
{
    UIScrollView *mainScroll;
    
    TextFieldView *selectField;
    TextFieldView *field1;
    TextFieldView *field2;
    
    UIButton *doneButton;
}
@end

@implementation FetusViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    navigationBarView.title = @"胎儿计算器";
    [navigationBarView setNormalLeftMenuButton];
    [navigationBarView setNormalRightButton];
    
    mainScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, NAVIGATIONBAR_HEIGHT, CURRENT_CONTENT_WIDTH, CURRENT_CONTENT_HEIGHT-NAVIGATIONBAR_HEIGHT)];
    [self.view addSubview:mainScroll];
    
    field1 = [[TextFieldView alloc] init];
    [field1 setBgImage:@"输入框.png" titleImage:@"完善资料页-身高icon" tip:@"输入宫高"];
    [mainScroll addSubview:field1];
    [field1 setCenter:CGPointMake(mainScroll.frame.size.width/2, 34+field1.frame.size.height/2)];
    
    //
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:field1.frame];
    [mainScroll addSubview:button];
    [button addTarget:self action:@selector(selectSG) forControlEvents:UIControlEventTouchUpInside];
    
    field2 = [[TextFieldView alloc] init];
    [field2 setBgImage:@"输入框.png" titleImage:@"完善资料页-孕中-选中.png" tip:@"输入腹围"];
    [mainScroll addSubview:field2];
    [field2 setCenter:CGPointMake(field1.center.x, field1.frame.size.height+field1.frame.origin.y+16+field2.frame.size.height/2)];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button1 setFrame:field2.frame];
    [mainScroll addSubview:button1];
    [button1 addTarget:self action:@selector(selectFW) forControlEvents:UIControlEventTouchUpInside];
    
    doneButton = [OftenMenthod setButtonTitle:@"确定"];
    [mainScroll addSubview:doneButton];
    [doneButton addTarget:self action:@selector(doneButt) forControlEvents:UIControlEventTouchUpInside];
    [doneButton setCenter:CGPointMake(field2.center.x, field2.frame.size.height+field2.frame.origin.y+44+doneButton.frame.size.height/2)];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
    field1.textField.text = nil;
    field2.textField.text = nil;
    [[mainScroll viewWithTag:110] removeFromSuperview];
    [[self.view viewWithTag:111] removeFromSuperview];
    [mainScroll setContentOffset:CGPointMake(0, 0)];
}

-(void)getSelectView:(NSInteger)type
{
    UIView *view = self.view;
    SelectPickerView *dateView = [[SelectPickerView alloc] initWithFrame:view.bounds];
    [dateView setDelegate:self];
    [dateView setTag:111];
    [dateView setShowView:type text:selectField.textField.text];
    [view addSubview:dateView];
}

-(void)selectSG
{
    CLog(@"输入宫高");
    selectField = field1;
    [self getSelectView:GG_TYPE];
}

-(void)selectFW
{
    CLog(@"选中腹围");
    selectField = field2;
    [self getSelectView:FW_TYPE];
}

-(void)doneButt
{
    //胎儿体重（ｇ）＝２９００＋０．３×宫高（cm）×腹围（cm）
    if (field1.textField.text.length == 0) {
        [LoadingFrameView autoDisappearTipview:@"请输入宫高"];
    }else if (field2.textField.text.length == 0){
        [LoadingFrameView autoDisappearTipview:@"请输入腹围"];
    }else{
    
        CGFloat gg = [[field1.textField.text substringToIndex:field1.textField.text.length-2] floatValue];
        CGFloat fw = [[field2.textField.text substringToIndex:field1.textField.text.length-2] floatValue];
        NSString *tz = [NSString stringWithFormat:@"%.1f",2900+0.3*gg*fw];
        CLog(@"%f==%f====%@",gg,fw,tz);
        [self showResult:tz];
    }
}

-(void)showResult:(NSString*)text
{
    [[mainScroll viewWithTag:110] removeFromSuperview];
    
    UIView *resultView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(doneButton.frame)+20, mainScroll.frame.size.width, 200)];
    [resultView setTag:110];
    [mainScroll addSubview:resultView];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"result_bg.png"]];
    [imageView sizeToFit];
    [imageView setCenter:CGPointMake(15+imageView.frame.size.width/2, imageView.frame.size.height/2)];
    [resultView addSubview:imageView];
    
    UIImageView *imageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"kg.png"]];
    [imageView1 sizeToFit];
    [imageView1 setCenter:CGPointMake(35+imageView1.frame.size.width/2, CGRectGetMaxY(imageView.frame)-imageView1.frame.size.height/2+10)];
    [resultView addSubview:imageView1];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    [label setTextColor:GET_COLOR(@"626262")];
    [label setFont:[UIFont boldSystemFontOfSize:13]];
    NSString *content = [NSString stringWithFormat:@"估计胎儿重量：%@g",text];
    label.attributedText = [content returnNSMutableAttributedString:[NSString stringWithFormat:@"%@g",text]];
    [label sizeToFit];
    [label setCenter:CGPointMake(CGRectGetMaxX(imageView1.frame)+10+label.frame.size.width/2, imageView1.center.y)];
    [resultView addSubview:label];

    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 245, 50)];
    [bgView setBackgroundColor:GET_COLOR(@"f6f6f6")];
    [bgView.layer setCornerRadius:4];
    [bgView setCenter:CGPointMake(mainScroll.frame.size.width/2, CGRectGetMaxY(imageView1.frame)+15+bgView.frame.size.height/2)];
    [resultView addSubview:bgView];
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, bgView.frame.size.width-20, bgView.frame.size.height)];
    [label1 setTextColor:GET_COLOR(@"626262")];
    [label1 setBackgroundColor:[UIColor clearColor]];
    [label1 setFont:[UIFont systemFontOfSize:11]];
    [label1 setNumberOfLines:0];
    [label1 setText:@"通过孕妈妈的宫高、腹围，可以简单估计胎宝宝的大小。"];
    [bgView addSubview:label1];
    
    CLog(@"%@===%f",doneButton,CGRectGetMaxY(doneButton.frame));
}

#pragma mark - SelectPickerViewDelegate
//选择的时间
-(void)selectedText:(NSString*)text
{
    CLog(@"选中的=====%@",text);
    selectField.textField.text = text;
}

@end
